import React, { useState, createContext } from "react";

const NumContext = createContext();

function Sub() {
  return (
    <NumContext.Consumer>
      {
        ({num,setNum}) => (
            <>
            <h2>{num}</h2>
            <h2><button onClick={()=>setNum(num+1)}>累加</button></h2>
            </>
       )
      }
    </NumContext.Consumer>
  );
}
function Father() {
  return <Sub />;
}
export default function Fuzisun() {
  const [num, setNum] = useState(1);

  return (
    <NumContext.Provider value={{num,setNum}}>
      <Father />
    </NumContext.Provider>
  );
}
 